﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common.ExtensionMethods
{
    /// <summary>
    /// 字符串的扩展方法
    /// </summary>
    /// <remarks>create by chenjinjian.com on 2015-01-13</remarks>
    public static class StringExtension
    {
        /// <summary>
        /// 获取省略的手机号码
        /// </summary>
        /// <param name="phone"></param>
        /// <remarks>create by chenjinjian.com on 2015-01-13</remarks>
        /// <returns></returns>
        public static string GetPhone(this string phone)
        {
            if (String.IsNullOrEmpty(phone))
            {
                return "";
            }
            else
            {
                if (phone.Length < 7)
                {
                    return phone;
                }
                return Regex.Replace(phone, @"(?im)(\d{3})(\d{4})(\d{4})", "$1****$3");
            }
        }

        /// <summary>
        /// 获取对应的枚举变量
        /// 使用这个方法必须判断不为空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="e">不为空,不为null</param>
        /// <remarks>create by chenjinjian.com on 2015-01-13</remarks>
        /// <returns></returns>
        public static T ConvertToEnum<T>(this string e)
        {
            return (T)Enum.Parse(typeof(T), e);
        }


        public static string GetEmial(this string str)
        {
            if (String.IsNullOrEmpty(str))
            { return ""; }
            else
            {
                if (str.Length < 7)
                {
                    return str;
                }
                return Regex.Replace(str, @"(?<=\S\S*?)[^@\s](?=\S*?@\S+)", "*");
            }
        }

        public static string GetCutStr(this string str, int length)
        {
            if (str.Length <= length)
            {
                return str;
            }
            return str.Remove(length, str.Length - length) + "...";
        }

        public static string GetCutStrStar(this string str, int length)
        {
            if (str.Length <= length)
            {
                return str;
            }
            return str.Remove(length, str.Length - length) + "***";
        }
    }
}
